package com.central.common.lb.chooser;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @ClassName RandomRuleChooser
 * @Description 随机的选择器
 * @author: puyunqing
 * @Date: 2025/3/21
 */
@Slf4j
public class RandomRuleChooser implements IRuleChooser {


    @Override
    public ServiceInstance choose(List<ServiceInstance> serviceInstances) {
        if (CollectionUtil.isNotEmpty(serviceInstances)) {
            int randomValue = ThreadLocalRandom.current().nextInt(serviceInstances.size());
            ServiceInstance serviceInstance = serviceInstances.get(randomValue);
            log.info("选择了ip为{}, 端口为: {}的服务器", serviceInstance.getHost(), serviceInstance.getPort());
            return serviceInstance;
        }
        return null;
    }
}
